package com.urbane.generator.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtils {

    public static void zipDirectory(File sourceDir, ZipOutputStream zos, String parentPath) throws IOException {
        for (File file : Objects.requireNonNull(sourceDir.listFiles())) {
            if (file.isDirectory()) {
                zipDirectory(file, zos, parentPath + file.getName() + "/");
            } else {
                ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
                zos.putNextEntry(zipEntry);
                Files.copy(file.toPath(), zos);
                zos.closeEntry();
            }
        }
    }

    public static void deleteDirectory(File file) {
        if (file.isDirectory()) {
            for (File child : Objects.requireNonNull(file.listFiles())) {
                deleteDirectory(child);
            }
        }
        file.delete();
    }
}